var ClubGroup_Hehuorenchengyuan = Item.extend({
    _ccsFileName: res.ClubGroup_Hehuorenchengyuan_json,
    _clubId: null,
    _tableView: null,
    _tableViewList: [],
    _groupData: [],
    _memberData: [],
    _partnerData: [],
    _curPartnerUid: null,
    _partnerUids: [],
    _onlineNum:0,

    initItem: function (clubId) {
        this._clubId = clubId;
        var clubInfo = ClubDataManager.getClubData(this._clubId);
        this._initTableView();

    },
    _initClickListeners: function () {
        this._clickListeners = {
            'nodeBottom.btn_search': '_clickSearch',
            'btn_showList': '_showPartnerList',
        };
    },
    _initEventListeners: function () {
        this._eventListeners = {};
        this._eventListeners[ClubMsgCode.AllMemberInfo] = '_allMemberInfoHandler';
        this._eventListeners[ClubMsgCode.QueryClubGroup] = '_queryClubGroupHandler';
    },
    /**
     * 刷新界面显示
     */
    refreshView: function () {
        this._groupData = [];
        this._memberData = [];
        this._partnerData = [];
        this._tableViewList = [];
        this._curPartnerUid = null;
        this._changeFilter();
    },
    //主动请求
    _changeFilter: function () {
        //1.请求分组列表QueryClubGroup
        clubMsgManager.requestGroups(this._clubId, true);
        //this._queryClubGroupHandler()
    },
    /**
     * 回调分组信息
     * @param data
     * @private
     */
    _queryClubGroupHandler: function(data){
        this._groupData = data.info.groups;
        //2.已成功后再请求成员列表allMemberInfo
        network.send(2103, {cmd: ClubMsgCode.AllMemberInfo, club_id: this._clubId, op: 2});
    },
    /**
     * 回调用户列表
     * @param data
     * @private
     */
    _allMemberInfoHandler:function(data){
        this._memberData = data.info.member;
        //成员数据
        //3.组装数据

        //合伙人列表数据
        var memberData = this._memberData;
        var groupData = this._groupData;
        var partnerData = this._partnerData;
        var groupLen = groupData.length;
        var memberLen = memberData.length;
        var partnerUids = this._partnerUids;

        var tmpLeaderData;
        for (var i = 0; i < groupLen; i++){
            tmpLeaderData = groupData[i];
            if (Number(groupData[i].op_uid) > 0){
                partnerData.push({uid:tmpLeaderData.op_uid, pos: tmpLeaderData.pos});
                partnerUids.push(tmpLeaderData.op_uid);
            }
        }

        //合伙人信息
        var partnerLen = partnerData.length;
        if (partnerLen == 0) {
            this._nodeList['nodeCurSelect'].setVisible(false);
            this._nodeList['cloneNode'].setVisible(false);
            return;
        }
        else {
            this._nodeList['nodeCurSelect'].setVisible(true);
        }
        var tmpMemberData;
        for (var i = 0; i < memberLen; i++){
            tmpMemberData = memberData[i];
            if (partnerUids.indexOf(tmpMemberData.u) > -1){
                for (var j = 0; j < partnerLen; j++){
                    if (tmpMemberData.u == partnerData[j].uid){
                        partnerData[j].name = tmpMemberData.n;
                        partnerData[j].headimgurl = tmpMemberData.h;
                    }
                }
            }
        }
        //设置合伙人列表
        this._initPartnerList();
    },
    /**
     * 初始化TableView
     */
    _initTableView: function () {
        var nodeContent = this._nodeList['nodeContent'];
        var tableViewSize = nodeContent.getContentSize();
        this._tableView = new cc.TableView(this, cc.size(tableViewSize.width, tableViewSize.height));
        this._tableView.setDelegate(this);
        this._tableView.setDirection(cc.SCROLLVIEW_DIRECTION_VERTICAL);
        this._tableView.setVerticalFillOrder(cc.TABLEVIEW_FILL_TOPDOWN);
        nodeContent.addChild(this._tableView);
        this._tableView.setPosition(cc.p(0, 0));
    },
    tableCellAtIndex: function (table, idx) {
        var self = this;
        var cell = table.dequeueCell();
        var node = null;
        if (cell == null) {
            cell = new cc.TableViewCell();
            node = addItem('ClubGroup_Hehuorenchengyuan_Item', cell, this._tableViewList[idx], this._clubId);
            node.setName("itemCell");
        } else {
            node = cell.getChildByName("itemCell");
            node.setData(this._tableViewList[idx]);
        }
        return cell;
    },
    tableCellTouched: function (table, cell) {
    },
    tableCellSizeForIndex: function (table, idx) {
        return cc.size(1016, 64);
    },
    numberOfCellsInTableView: function (table) {
        return this._tableViewList.length;
    },

    /**
     * 初始合伙人列表
     * @private
     */
    _initPartnerList: function () {
        var cloneNode = this._nodeList['cloneNode'];
        cloneNode.setVisible(false);

        if (this._curPartnerUid == null)
            this._initCurPartner(this._partnerData[0].uid);
        else
            this._initCurPartner(this._curPartnerUid);
    },
    /**
     * 生成合伙人列表
     * @private
     */
    _genPartnerList: function(){
        var self = this;

        var partnerList = this._nodeList['nodeList'];
        var cloneNode = this._nodeList['cloneNode'];
        cloneNode.setVisible(true);
        partnerList.removeAllChildren();
        var partnerData = this._partnerData;

        var singleParterData;
        for (var i = 0; i < partnerData.length; i++){
            singleParterData = partnerData[i];
            if (singleParterData.uid == this._curPartnerUid) continue;
            var custom_item = new ccui.Layout();
            custom_item.setContentSize(cc.size(342, 94));
            partnerList.pushBackCustomItem(custom_item);

            var cloneObj = duplicateNode(cloneNode);
            custom_item.addChild(cloneObj);
            cloneObj.setAnchorPoint(0, 0);
            cloneObj.setPosition(cc.p(16, 47));
            cloneObj.getChildByName('btn_cell').uid = singleParterData.uid;
            TouchUtil.setOnclickListener(cloneObj.getChildByName('btn_cell'), function (sender) {
                self._initCurPartner(sender.uid);
            });
            loadHeadImage(singleParterData.headimgurl, cloneObj.getChildByName('head'), true);//头像
            cloneObj.getChildByName('txt_name').setString(singleParterData.name);
            cloneObj.getChildByName('txt_id').setString(singleParterData.uid);
        }

        cloneNode.setVisible(false);
    },

    /**
     * 初始当前合伙人
     * @private
     */
    _initCurPartner: function(uid){
        //设置列表中的第一个为当前合伙人
        if (this._curPartnerUid == uid) return;
        this._curPartnerUid = uid;
        this._nodeList['nodeList'].setVisible(false);
        var curPartnerNode = this._nodeList['nodeCurSelect'];
        var curPartnerData = null;
        for (var i = 0; i < this._partnerData.length; i++){
            if (this._partnerData[i].uid == uid){
                curPartnerData = this._partnerData[i];
            }
        }
        if (curPartnerData != null) {
            loadHeadImage(curPartnerData.headimgurl, curPartnerNode.getChildByName('head'), true);//头像
            curPartnerNode.getChildByName('txt_name').setString(curPartnerData.name);
            curPartnerNode.getChildByName('txt_id').setString(curPartnerData.uid);
        }

        //清空搜索的内容
        this._nodeList['nodeBottom.input_search'].setString('');
        //对应的绑定信息和解绑信息
        this._setGroupMemberList();
        this._updateOnlineNum();
        this._tableView.reloadData();
    },
    /**
     * 展示合伙人列表
     * @private
     */
    _showPartnerList: function () {
        var showListNode = this._nodeList['nodeList'];
        showListNode.setVisible(!showListNode.isVisible());
        if (showListNode.isVisible()){
            this._genPartnerList();
        }
    },

    _clickSearch: function () {
        this._setGroupMemberList();
        this._tableView.reloadData();
    },

    /**
     * 获得对应的组的数据
     * @private
     */
    _setGroupMemberList:function () {
        var groupList = [];
        var curGroupId = this._curPartnerUid;
        var filterStr = this._nodeList['nodeBottom.input_search'].getString();
        var isCond = (filterStr == '' || filterStr.length == 0) ? false : true;

        var curGroupUids = [];
        for (var i = 0; i < this._groupData.length; i++){
            if (this._groupData[i].op_uid == curGroupId) {
                curGroupUids = this._groupData[i].uids;
                break;
            }
        }
        curGroupUids.unshift(this._curPartnerUid);


        var memberData = this._memberData;
        var memberLen = memberData.length;
        var locationIdx;
        this._onlineNum = 0;
        for (var i = 0; i < memberLen; i++){
            locationIdx = curGroupUids.indexOf(memberData[i].u);
            if (locationIdx > -1 && isCond == false){
                groupList.push(memberData[i]);
                memberData[i].isSelected = false;
                memberData[i].loadObj = this;
                memberData[i].partnerUid = this._curPartnerUid;
                if (memberData[i].s == 1) this._onlineNum++;
            }
            else if (locationIdx > -1 && isCond == true && memberData[i].n.indexOf(filterStr) > -1){
                groupList.push(memberData[i]);
                memberData[i].isSelected = false;
                memberData[i].loadObj = this;
                memberData[i].partnerUid = this._curPartnerUid;
                if (memberData[i].s == 1) this._onlineNum++;
            }
        }
        this._tableViewList = groupList;
    },
    /**
     * 更新在线人数
     * @private
     */
    _updateOnlineNum: function () {
        this._nodeList['nodeBottom.txt_num'].setString(this._onlineNum + '/' + this._tableViewList.length);
    },
});